<?php
/**
 * Ecart
 *
 * This file is part of Ecart.
 *
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category    Ecart
 * @package     Ecart_Account
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 *
 * @category    Ecart
 * @package     Ecart_Account
 * @subpackage  Model
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Account_Model_Form_ChangeInfo extends Ecart_Form
{
    protected $_translatorModule = 'account';

    public function __construct($options = array())
    {
        $default = array(
            'id' => 'form-info',
            'action' => Zend_Controller_Front::getInstance()->getBaseUrl()
                 . Ecart_Locale::getLanguageUrl() . '/account/info/change'
        );
        if (null !== $options || !count($options)) {
            $default = array_merge($default, $options);
        }
        parent::__construct($default);
        $db = Ecart::db();

        $this->addElement('text', 'email', array(
            'required'  => true,
            'label'     => 'Email',
            'class'     => 'input-text required email',
            'validators' => array(
                'EmailAddress',
                new Ecart_Validate_Exists(
                    Ecart::single('account/customer'),
                    'email',
                    "id <> " . Ecart::getCustomerId()
                )
            )
        ));
        $this->addElement('text', 'firstname', array(
            'required' => true,
            'label' => 'Firstname',
            'class' => 'input-text required'
        ));
        $this->addElement('text', 'lastname', array(
            'required' => true,
            'label' => 'Lastname',
            'class' => 'input-text required'
        ));
        $this->addDisplayGroup(array('email', 'firstname', 'lastname'), 'login', array(
            'legend' => 'General information'
        ));
        $this->getDisplayGroup('login')
            ->addRow('email', 'row1')
            ->addRow(array('firstname', 'lastname'), 'row2');
        $this->getDisplayGroup('login')->getRow('row2')
            ->addColumn('firstname', 'col1')
            ->addColumn('lastname', 'col2');

        $rows = Ecart::single('account/customer_field')
            ->getFields();
        $groupsFields = array();
        foreach ($rows as $row) {
            $field = 'field_' . $row['id'];
            $config = array(
                'id' => 'field_' . $row['name'],
                'required' => (boolean) $row['required'],
                'label'    => $row['field_label'],
                'class'    => 'input-text'
            );
            if ($row['field_type'] == 'textarea') {
                $config['rows'] = 6;
                $config['cols'] = 60;
            }
            $this->addElement($row['field_type'], $field, $config);

            if ($row['required']) {
                $this->getElement($field)
                    ->addValidator('NotEmpty')
                    ->setAttrib(
                        'class',
                        $this->getElement($field)
                            ->getAttrib('class') . ' required'
                    );
            }
            if (!empty($row['validator'])) {
                $this->getElement($field)->addValidator($row['validator']);
                if ($row['validator'] == 'Date') {
                    $this->getElement($field)
                        ->setAttrib(
                            'class' ,
                            $this->getElement($field)
                                ->getAttrib('class') . ' input-date'
                        );
                }
            }
            if (!empty($row['ecart_validator'])) {
                $this->getElement($field)
                    ->addValidator(new $row['ecart_validator']());
            }
            if (isset($row['customer_valueset_id'])) {
                $values = Ecart::single('account/Customer_ValueSet_Value')
                    ->getCustomValues(
                        $row['customer_valueset_id'],
                        Ecart_Locale::getLanguageId()
                    );
                $element = $this->getElement($field);
                if (method_exists($element, 'setMultiOptions')) {
                    $element->setMultiOptions($values);
                }

            }
            $groupsFields[$row['customer_field_group_id']][$row['id']] = $field;
        }

        /* add field groups */
        if (count($groupsFields)) {
            $groups = Ecart::single('account/customer_fieldGroup')
                ->getCustomGroups(
                    array_keys($groupsFields), Ecart_Locale::getLanguageId()
                );
            foreach ($groups as $row) {
                $this->addDisplayGroup(
                    array_values($groupsFields[$row['id']]),
                    empty($row['name']) ? $row['id'] : $row['name'],
                    array('legend' => $row['group_label'])
                );
                $this->getDisplayGroup(empty($row['name']) ? $row['id'] : $row['name'])
                    ->setDisableTranslator(true);
            }
        }

        $this->addElement('checkbox', 'change_password_toggle', array(
            'label'     => 'Change password',
            'onchange'  => "togglePasswordForm(this.checked)"
        ));
        $this->getElement('change_password_toggle')
            ->addDecorator('Label', array(
                'tag' => '',
                'placement' => 'append',
                'separator' => ''
            ))
            ->addDecorator('HtmlTag', array(
                'tag' => 'div',
                'class' => 'label-inline'
            ));
        $this->addElement('password', 'password', array(
            'disabled' => 'disabled',
            'label'    => 'New password',
            'class'    => 'input-text required password',
            'validators' => array(
                'NotEmpty',
                new Ecart_Validate_PasswordConfirmation()
            )
        ));
        $this->addElement('password', 'password_confirm', array(
            'disabled' => 'disabled',
            'label'    => 'Confirm new password',
            'class'    => 'input-text required password'
        ));
        $this->addElement('password', 'password_current', array(
            'disabled' => 'disabled',
            'label'    => 'Current password',
            'class'    => 'input-text required password',
            'validators' => array(
                'NotEmpty',
                new Ecart_Validate_PasswordEqual(
                    Ecart::single('account/customer')
                        ->getPasswordById(Ecart::getCustomerId())
                )
            )
        ));

        $this->addDisplayGroup(
            array('password_current', 'password', 'password_confirm'),
            'change_password',
            array(
                'legend' => 'Change password',
                'style'  => 'display: none;'
            )
        );
        $this->getDisplayGroup('change_password')
            ->addRow(
                'password_current',
                'row1',
                array('colsetClass' => 'col2-set')
            )
            ->addRow(array('password', 'password_confirm'), 'row2');

        $this->getDisplayGroup('change_password')
            ->getRow('row2')
            ->addColumn('password', 'col1')
            ->addColumn('password_confirm', 'col2');

        $this->addElement('button', 'submit', array(
            'type' => 'submit',
            'class' => 'button',
            'label' => 'Save'
        ));

        $this->addActionBar(array('submit'));
    }
}